/*
 *  Copyright 2010 Ancora Research Group.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

/*
 * Main.java
 *
 * Created on 22/Set/2010, 16:35:13
 */

package org.ancora.SharedLibrary.AppBase.SimpleGui;

import java.io.File;
import java.util.Arrays;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import org.ancora.SharedLibrary.AppBase.App;
import org.ancora.SharedLibrary.AppBase.AppOptionFile.AppOptionFile;

/**
 *
 * @author Ancora Group <ancora.codigo@gmail.com>
 */
public class MainWindow extends javax.swing.JFrame {

    /** Creates new form Main */
    public MainWindow(App application) {
        initComponents();

        // Set last used file
        Preferences prefs = Preferences.userNodeForPackage(MainWindow.class);
        String lastFile =prefs.get(OPTION_LAST_USED_FILE, "");

        filenameTextField.setText(lastFile);

        // Init file chooser
        fc = new JFileChooser();

        // Redirect output to jtextfiled
        Logger logger = Logger.getLogger("");
//        logger.addHandler(new JTextAreaHandler(outputArea));

        this.application = application;

        // Init buttons
        setButtonsEnable(true);
        worker = null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jLabel1 = new javax.swing.JLabel();
      jScrollPane1 = new javax.swing.JScrollPane();
      outputArea = new javax.swing.JTextArea();
      filenameTextField = new javax.swing.JTextField();
      browseButton = new javax.swing.JButton();
      startButton = new javax.swing.JButton();
      cancelButton = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setResizable(false);

      jLabel1.setText("Indicate a Job file:");

      outputArea.setColumns(20);
      outputArea.setEditable(false);
      outputArea.setRows(5);
      jScrollPane1.setViewportView(outputArea);

      browseButton.setText("Browse...");
      browseButton.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            browseButtonActionPerformed(evt);
         }
      });

      startButton.setText("Start");
      startButton.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            startButtonActionPerformed(evt);
         }
      });

      cancelButton.setText("Cancel");
      cancelButton.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            cancelButtonActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(filenameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(browseButton)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(startButton)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(cancelButton)
            .addContainerGap(298, Short.MAX_VALUE))
         .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE)
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel1)
               .addComponent(filenameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(browseButton))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(startButton)
               .addComponent(cancelButton))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
       // Clear text area
       outputArea.setText("");

       // Check if file is valid
       String filename = filenameTextField.getText();
       File file = new File(filename);
       if(!file.exists()) {
          System.out.println("File '"+filename+"' does not exist.");
          File file2 = new File("./");
          System.out.println("Current files in folder '"+file2.getAbsolutePath()+"':");
          System.out.println(Arrays.asList(file2.listFiles()));
          //outputArea.append("File '"+filename+"' does not exist.\n");
          return;
       }

       // Save accessed file
       Preferences prefs = Preferences.userNodeForPackage(MainWindow.class);
       prefs.put(OPTION_LAST_USED_FILE, filename);

       // Get Options from file
       AppOptionFile optionFile = AppOptionFile.parseFile(file, application.getAppOptionEnum());
       if(optionFile == null) {
          System.out.println("Could not load options from '"+filename+"'.");
          //outputArea.append("Could not load options from '"+filename+"'.");
          return;
       }

 //      worker = new ApplicationWorker(this, optionFile.getMap());
       //ApplicationWorker runner = new ApplicationWorker(this, optionFile.getMap());
       //ApplicationWorker runner = new ApplicationWorker(this, filename);
       //runner.execute();
//       workerExecutor = Executors.newSingleThreadExecutor();
//       workerExecutor.submit(worker);
       

    }//GEN-LAST:event_startButtonActionPerformed

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed

       fc.setCurrentDirectory(new File(filenameTextField.getText()));
       int returnVal = fc.showOpenDialog(this);

       if (returnVal == JFileChooser.APPROVE_OPTION) {
          File file = fc.getSelectedFile();
          filenameTextField.setText(file.getAbsolutePath());
       }

    }//GEN-LAST:event_browseButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
       // Check if there is a worker
       if(worker == null) {
          outputArea.append("Application is not running.");
          return;
       }

       //boolean interruptIfRunning = true;
       //worker.cancel(interruptIfRunning);

       workerExecutor.shutdownNow();
       setButtonsEnable(true);
    }//GEN-LAST:event_cancelButtonActionPerformed



    /**
    * @param args the command line arguments
    */
    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SimpleGui().setVisible(true);
            }
        });
    }
     *
     */

    public final void setButtonsEnable(boolean enable) {
       browseButton.setEnabled(enable);
       startButton.setEnabled(enable);
       cancelButton.setEnabled(!enable);
    }

   public App getApplication() {
      return application;
   }



   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton browseButton;
   private javax.swing.JButton cancelButton;
   private javax.swing.JTextField filenameTextField;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTextArea outputArea;
   private javax.swing.JButton startButton;
   // End of variables declaration//GEN-END:variables

   // Other variables
   private JFileChooser fc;
   final private App application;
   private ApplicationWorker worker;
   private ExecutorService workerExecutor;

   private static final String OPTION_LAST_USED_FILE = "lastUsedFile";
}
